package web;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author rick
 */
public class SessionStatus {
	private String authuser;
	private String user;
	private String upload_id;
	private String file_id;
	private String url;
	private String content_type;
	private long bytesTransferred;
	private long bytesTotal;

	public SessionStatus(String authuser, String user, String upload_id,
			String file_id, String url, String content_type) {
		this.authuser = authuser;
		this.user = user;
		this.upload_id = upload_id;
		this.file_id = file_id;
		this.url = url;
		this.content_type = content_type;
	}

	public String getJSON() {
		JSONObject json = new JSONObject();

		// add externalFieldTransfers
		JSONArray jsonArray = new JSONArray();
		jsonArray.put("\"name\":\"file\"");

		if (bytesTotal != bytesTransferred) {
			jsonArray.put("\"status\":\"IN_PROGRESS\"");
		} else {
			jsonArray.put("\"status\":\"COMPLETED\"");
		}

		jsonArray.put("\"bytesTransferred\":\"" + bytesTransferred + "\"");
		jsonArray.put("\"bytesTotal\":\"" + bytesTotal + "\"");

		// add the high level object
		JSONObject highLevelJSON = new JSONObject();
		try {
			// add the formPostInfo
			JSONObject formPostJSON = new JSONObject();
			formPostJSON.put("url", this.url);
			formPostJSON.put("content_type", this.content_type);

			// Is there a better way to do this?
			jsonArray.put("\"formPostInfo\":" + formPostJSON);

			highLevelJSON.put("state", "OPEN");
			highLevelJSON.put("externalFieldTransfers", jsonArray);

			highLevelJSON.put("upload_id", this.upload_id);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "\"sessionStatus\":" + highLevelJSON;
	}
}
